package db

import (
	"errors"
	"log"
	//"reflect"
	"server_cluster/battle/sharemem"
	//"strconv"
)

type Config_monster_attr struct {
	Id         int32 `gorm:"primary_key;column:id;autoIncrement:false" csv:"Id"`
	GearScore  int32 `gorm:"column:GearScore" csv:"GearScore"`
	Atk        int32 `gorm:"column:Atk" csv:"Atk,omitempty"`
	Hp         int32 `gorm:"column:Hp" csv:"Hp"`
	Def        int32 `gorm:"column:Def" csv:"Def,omitempty"`
	Tough      int32 `gorm:"column:Tough" csv:"Tough,omitempty"`
	DefPen     int32 `gorm:"column:DefPen" csv:"DefPen,omitempty"`
	DefRes     int32 `gorm:"column:DefRes" csv:"DefRes,omitempty"`
	Anger      int32 `gorm:"column:Anger" csv:"Anger,omitempty"`
	Crit       int32 `gorm:"column:Crit" csv:"Crit,omitempty"`
	CritRes    int32 `gorm:"column:CritRes" csv:"CritRes,omitempty"`
	CritDmg    int32 `gorm:"column:CritDmg" csv:"CritDmg,omitempty"`
	CritDmgRes int32 `gorm:"column:CritDmgRes" csv:"CritDmgRes,omitempty"`
	FireRes    int32 `gorm:"column:FireRes" csv:"FireRes,omitempty"`
	IceRes     int32 `gorm:"column:IceRes" csv:"IceRes,omitempty"`
	WindRes    int32 `gorm:"column:WindRes" csv:"WindRes,omitempty"`
	DarkRes    int32 `gorm:"column:DarkRes" csv:"DarkRes,omitempty"`
	LightRes   int32 `gorm:"column:LightRes" csv:"LightRes,omitempty"`
	ChopRes    int32 `gorm:"column:ChopRes" csv:"ChopRes,omitempty"`
	PunctRes   int32 `gorm:"column:PunctRes" csv:"PunctRes,omitempty"`
	BludRes    int32 `gorm:"column:BludRes" csv:"BludRes,omitempty"`
	FluidRes   int32 `gorm:"column:FluidRes" csv:"FluidRes,omitempty"`
	FireInc    int32 `gorm:"column:FireInc" csv:"FireInc,omitempty"`
	IceInc     int32 `gorm:"column:IceInc" csv:"IceInc,omitempty"`
	WindInc    int32 `gorm:"column:WindInc" csv:"WindInc,omitempty"`
	DarkInc    int32 `gorm:"column:DarkInc" csv:"DarkInc,omitempty"`
	LightInc   int32 `gorm:"column:LightInc" csv:"LightInc,omitempty"`
	ChopInc    int32 `gorm:"column:ChopInc" csv:"ChopInc,omitempty"`
	PunctInc   int32 `gorm:"column:PunctInc" csv:"PunctInc,omitempty"`
	BludInc    int32 `gorm:"column:BludInc" csv:"BludInc,omitempty"`
	FluidInc   int32 `gorm:"column:FluidInc" csv:"FluidInc,omitempty"`
	ExCharge   int32 `gorm:"column:ExCharge" csv:"ExCharge,omitempty"`
	Speed      int32 `gorm:"column:Speed" csv:"Speed"`
	Drage      int32 `gorm:"column:Drage" csv:"Drage,omitempty"`
	DmgInc     int32 `gorm:"column:DmgResInc" csv:"DmgResInc,omitempty"`
	DmgDec     int32 `gorm:"column:DmgResDec" csv:"DmgResDec,omitempty"`
	DotDmg     int32 `gorm:"column:DotDmgRes" csv:"DotDmgRes,omitempty"`
	CureInc    int32 `gorm:"column:HealBoost" csv:"HealBoost,omitempty"`
	BeCureInc  int32 `gorm:"column:HealUp" csv:"HealUp,omitempty"`
	FirePen    int32 `gorm:"column:FirePen" csv:"FirePen,omitempty"`
	IcePen     int32 `gorm:"column:IcePen" csv:"IcePen,omitempty"`
	WindPen    int32 `gorm:"column:WindPen" csv:"WindPen,omitempty"`
	DarkPen    int32 `gorm:"column:DarkPen" csv:"DarkPen,omitempty"`
	LightPen   int32 `gorm:"column:LightPen" csv:"LightPen,omitempty"`
	ChopPen    int32 `gorm:"column:ChopPen" csv:"ChopPen,omitempty"`
	PunctPen   int32 `gorm:"column:PunctPen" csv:"PunctPen,omitempty"`
	BludPen    int32 `gorm:"column:BludPen" csv:"BludPen,omitempty"`
	FluidPen   int32 `gorm:"column:FluidPen" csv:"FluidPen,omitempty"`
	Charge     int32 `gorm:"column:Charge" csv:"Charge,omitempty"`
	ExDmgInc   int32 `gorm:"column:ExDmgInc" csv:"ExDmgInc,omitempty"`
}

func (tmp *Config_monster_attr) TableName() string {
	return "config_monster_attr"
}

func (tmp *Config_monster_attr) getAll() ([]*Config_monster_attr, error) {
	var data []*Config_monster_attr
	result := Conn.Find(&data)

	return data, result.Error
}

func (tmp *Config_monster_attr) LoadData() error {
	data, err := tmp.getAll()
	if err != nil {
		return err
	}
	var myshm = &(sharemem.Myshm.Cfg.Mstat)
	for key, value := range data {
		if key >= sharemem.MONSTER_ATTR_MAX {
			return errors.New("config_monster_attr format error 8")
		}
		myshm[key].Id = value.Id
		i := 0
		myshm[key].At[i].Id = sharemem.GEARSCORE
		myshm[key].At[i].Val = value.GearScore
		i++
		myshm[key].At[i].Id = sharemem.ATK
		myshm[key].At[i].Val = value.Atk
		i++
		myshm[key].At[i].Id = sharemem.HP
		myshm[key].At[i].Val = value.Hp
		i++
		myshm[key].At[i].Id = sharemem.DEF
		myshm[key].At[i].Val = value.Def
		i++
		myshm[key].At[i].Id = sharemem.TOUGH
		myshm[key].At[i].Val = value.Tough
		i++
		myshm[key].At[i].Id = sharemem.DEFPEN
		myshm[key].At[i].Val = value.DefPen
		i++
		myshm[key].At[i].Id = sharemem.DEFRES
		myshm[key].At[i].Val = value.DefRes
		i++
		myshm[key].At[i].Id = sharemem.ANGER
		myshm[key].At[i].Val = value.Anger
		i++
		myshm[key].At[i].Id = sharemem.CRIT
		myshm[key].At[i].Val = value.Crit
		i++
		myshm[key].At[i].Id = sharemem.CRITRES
		myshm[key].At[i].Val = value.CritRes
		i++
		myshm[key].At[i].Id = sharemem.CRITDMG
		myshm[key].At[i].Val = value.CritDmg
		i++
		myshm[key].At[i].Id = sharemem.CRITDMGRES
		myshm[key].At[i].Val = value.CritDmgRes
		i++
		myshm[key].At[i].Id = sharemem.FIRERES
		myshm[key].At[i].Val = value.FireRes
		i++
		myshm[key].At[i].Id = sharemem.ICERES
		myshm[key].At[i].Val = value.IceRes
		i++
		myshm[key].At[i].Id = sharemem.WINDRES
		myshm[key].At[i].Val = value.WindRes
		i++
		myshm[key].At[i].Id = sharemem.DARKRES
		myshm[key].At[i].Val = value.DarkRes
		i++
		myshm[key].At[i].Id = sharemem.LIGHTRES
		myshm[key].At[i].Val = value.LightRes
		i++
		myshm[key].At[i].Id = sharemem.CHOPRES
		myshm[key].At[i].Val = value.ChopRes
		i++
		myshm[key].At[i].Id = sharemem.PUNCTRES
		myshm[key].At[i].Val = value.PunctRes
		i++
		myshm[key].At[i].Id = sharemem.BLUDRES
		myshm[key].At[i].Val = value.BludRes
		i++
		myshm[key].At[i].Id = sharemem.FLUIDRES
		myshm[key].At[i].Val = value.FluidRes
		i++
		myshm[key].At[i].Id = sharemem.FIREINC
		myshm[key].At[i].Val = value.FireInc
		i++
		myshm[key].At[i].Id = sharemem.ICEINC
		myshm[key].At[i].Val = value.IceInc
		i++
		myshm[key].At[i].Id = sharemem.WINDINC
		myshm[key].At[i].Val = value.WindInc
		i++
		myshm[key].At[i].Id = sharemem.DARKINC
		myshm[key].At[i].Val = value.DarkInc
		i++
		myshm[key].At[i].Id = sharemem.LIGHTINC
		myshm[key].At[i].Val = value.LightInc
		i++
		myshm[key].At[i].Id = sharemem.CHOPINC
		myshm[key].At[i].Val = value.ChopInc
		i++
		myshm[key].At[i].Id = sharemem.PUNCTINC
		myshm[key].At[i].Val = value.PunctInc
		i++
		myshm[key].At[i].Id = sharemem.BLUDINC
		myshm[key].At[i].Val = value.BludInc
		i++
		myshm[key].At[i].Id = sharemem.FLUIDINC
		myshm[key].At[i].Val = value.FluidInc
		i++
		myshm[key].At[i].Id = sharemem.EXCHARGE
		myshm[key].At[i].Val = value.ExCharge
		i++
		myshm[key].At[i].Id = sharemem.SPEED
		myshm[key].At[i].Val = value.Speed
		i++
		myshm[key].At[i].Id = sharemem.DRAGE
		myshm[key].At[i].Val = value.Drage
		i++
		myshm[key].At[i].Id = sharemem.DMGINC
		myshm[key].At[i].Val = value.DmgInc
		i++
		myshm[key].At[i].Id = sharemem.DMGDEC
		myshm[key].At[i].Val = value.DmgDec
		i++
		myshm[key].At[i].Id = sharemem.DOTDMG
		myshm[key].At[i].Val = value.DotDmg
		i++
		myshm[key].At[i].Id = sharemem.CUREINC
		myshm[key].At[i].Val = value.CureInc
		i++
		myshm[key].At[i].Id = sharemem.BECUREINC
		myshm[key].At[i].Val = value.BeCureInc
		i++
		myshm[key].At[i].Id = sharemem.FIREPEN
		myshm[key].At[i].Val = value.FirePen
		i++
		myshm[key].At[i].Id = sharemem.ICEPEN
		myshm[key].At[i].Val = value.IcePen
		i++
		myshm[key].At[i].Id = sharemem.WINDPEN
		myshm[key].At[i].Val = value.WindPen
		i++
		myshm[key].At[i].Id = sharemem.DARKPEN
		myshm[key].At[i].Val = value.DarkPen
		i++
		myshm[key].At[i].Id = sharemem.LIGHTPEN
		myshm[key].At[i].Val = value.LightPen
		i++
		myshm[key].At[i].Id = sharemem.CHOPPEN
		myshm[key].At[i].Val = value.ChopPen
		i++
		myshm[key].At[i].Id = sharemem.PUNCTPEN
		myshm[key].At[i].Val = value.PunctPen
		i++
		myshm[key].At[i].Id = sharemem.BLUDPEN
		myshm[key].At[i].Val = value.BludPen
		i++
		myshm[key].At[i].Id = sharemem.FLUIDPEN
		myshm[key].At[i].Val = value.FluidPen
		i++
		myshm[key].At[i].Id = sharemem.CHARGE
		myshm[key].At[i].Val = value.Charge
		i++
		myshm[key].At[i].Id = sharemem.EXDMGINC
		myshm[key].At[i].Val = value.ExDmgInc
		i++
		/*vr := reflect.ValueOf(*value)
		vr_len := reflect.TypeOf(*value).NumField()
		for i := 1;i<=sharemem.ATTR_MAX && i<vr_len;i++ {
			fv := vr.Field(i).Interface().(int32)
			if len(fv) > 0 {
				a_list := int32s.Split(fv, ";")
				if len(a_list) != 2 {
					return errors.New("config_monster_attr format error 9")
				}
				arg1, err := strconv.Atoi(a_list[0])
				if err != nil {
					return errors.New("config_monster_attr format error 7")
				}
				arg2, err := strconv.Atoi(a_list[1])
				if err != nil {
					return errors.New("config_monster_attr format error 6")
				}
				myshm[key].At[i-1].Id = int32(arg1)
				myshm[key].At[i-1].Val = int32(arg2)
				i++
			}
		}*/
		//
	}

	log.Println("load config_monster_attr completed.")

	return nil
}
